package com.yunji.order.business.delay;

import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.order.business
 * @Title: MyAsyncConfigurer
 * @Description: 线程池设置
 * <p>
 * History:
 * Date						Version			Author			Summary
 * ========================================================================
 * 2024-04-20 16:05:47		V1.0			HuaAo		新建类
 */
@Component
public class MyAsyncConfigurer implements AsyncConfigurer {
    @Override
    public ThreadPoolTaskExecutor getAsyncExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        // 设置核心线程数,默认为1
        pool.setCorePoolSize(2);
        // 当核心线程都在跑任务，还有多余的任务会存到此处。
        pool.setQueueCapacity(30);
        // 最大线程数，默认为Integer.MAX_VALUE，
        // 如果queueCapacity存满了，还有任务就会启动更多的线程，
        // 直到线程数达到maxPoolSize。如果还有任务，则根据拒绝策略进行处理。
        pool.setMaxPoolSize(8);
        // 设置线程活跃时间（秒）
        pool.setKeepAliveSeconds(120);
        pool.setWaitForTasksToCompleteOnShutdown(true);
        pool.setAwaitTerminationSeconds(60 * 15);
        pool.setThreadNamePrefix("MyAsync-");
        pool.initialize();
        return pool;
    }

}